﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Zr
{
    public partial class SelectStuInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();//标题
                Bind2();
                //Bind3();
            }
        }

        private void Bind()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.schoolname from School a,Zhuren b where zhurenID='" + Session["zhurenid"].ToString() + "' and a.schoolid=b.schoolid";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            this.TdTitle.InnerHtml = ds.Tables[0].Rows[0][0].ToString() + "的学生信息查询";//标题

        }

        private void Bind2()
        {

            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select b.className,b.classID from Zhuren a,class b where zhurenID='" + Session["zhurenid"].ToString() + "' and a.schoolID=b.schoolID";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader ds = cmd.ExecuteReader();   //读取形式，也可以是用Dataset的形式
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "className";   //绑定班级
            DropDownList1.DataValueField = "classID"; //绑定班级编号
            DropDownList1.DataBind();
        }

        private void Bind3()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.stuid,a.stuname,a.sex,a.age,c.classname from student a,zhuren b,class c where zhurenID='" + Session["zhurenid"].ToString() + "' and b.schoolid=c.schoolid and a.classid=c.classid";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind3();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.stuid,a.stuname,a.sex,a.age,c.classname from student a,zhuren b,class c where zhurenID='" + Session["zhurenid"].ToString() + "' and b.schoolid=c.schoolid and a.classid=c.classid and a.classid='" + this.DropDownList1.SelectedValue + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        /// <summary>
        /// 全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            Bind3();
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            //调用导出函数
            ExitGV("application/ms-excel", "学生信息.xls", this.GridView1);
        }

        private void ExitGV(string FileType, string FileName, GridView gv)
        {
            //功能：将当前的gv中的内容导出到Excel
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
            Response.ContentType = FileType;
            this.EnableViewState = false;
            System.IO.StringWriter tw;
            tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw;
            hw = new HtmlTextWriter(tw);
            gv.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();

        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            //..... 
        }
    }
}